package com.movie.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 16-购买兑换券
 * 
 */
public class BuyTicketExchangeAct extends BaseActivity {
	private Context mContext = BuyTicketExchangeAct.this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.buyticketexchangeact);
		initBackTitleBar();
		onClick();
		setZhangshu();
	}

	private ImageView quzhifu = null;
	
	
	private int[] backgrounds = new int[] { R.drawable.jia2, R.drawable.jia,
			R.drawable.jian2, R.drawable.jian };
	private ImageView jian = null;
	private ImageView jia = null;
	private TextView zhangshu = null;

	private void onClick() {

		quzhifu = (ImageView) findViewById(R.id.quzhifu);
		quzhifu.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(mContext,
						TicketExchangeSuccessAct.class);
				mContext.startActivity(intent);
			}
		});

		jian = (ImageView) findViewById(R.id.jian);
		jia = (ImageView) findViewById(R.id.jia);
		zhangshu = (TextView) findViewById(R.id.zhangshu);

		jia.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				addLogic();
			}

		});
		;
		jian.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				decreaseLogic();
			}

		});
	}

	private void decreaseLogic() {
		if (!"".equals(zhangshu.getText().toString())) {
			int zhangshuNum = Integer.parseInt(zhangshu.getText().toString());
			if (zhangshuNum > 0) {
				zhangshuNum -= 1;
				zhangshu.setText(zhangshuNum + "");
				if (zhangshuNum == 0) {
					jia.setBackgroundResource(backgrounds[1]);
					jia.setEnabled(true);
					jian.setBackgroundResource(backgrounds[2]);
					jian.setEnabled(false);
				} else if (zhangshuNum < 4) {
					jia.setBackgroundResource(backgrounds[1]);
					jia.setEnabled(true);
				}
			}
		}
	}

	private void setZhangshu() {
		if (!"".equals(zhangshu.getText().toString())) {
			int zhangshuNum = Integer.parseInt(zhangshu.getText().toString());
			if (zhangshuNum == 0) {
				jia.setBackgroundResource(backgrounds[1]);
				jia.setEnabled(true);
				jian.setBackgroundResource(backgrounds[2]);
				jian.setEnabled(false);
			}
			if (zhangshuNum == 4) {
				jia.setBackgroundResource(backgrounds[0]);
				jia.setEnabled(false);
				jian.setBackgroundResource(backgrounds[3]);
				jian.setEnabled(true);
			}
		}
	}

	private void addLogic() {
		if (!"".equals(zhangshu.getText().toString())) {
			int zhangshuNum = Integer.parseInt(zhangshu.getText().toString());
			if (zhangshuNum < 4) {
				zhangshuNum += 1;
				zhangshu.setText(zhangshuNum + "");
				if (zhangshuNum == 4) {
					jia.setBackgroundResource(backgrounds[0]);
					jia.setEnabled(false);
					jian.setBackgroundResource(backgrounds[3]);
					jian.setEnabled(true);
				} else if (zhangshuNum > 0) {
					jian.setBackgroundResource(backgrounds[3]);
					jian.setEnabled(true);
				}
				// jian.setBackgroundResource(backgrounds[1]);
				// jian.setEnabled(false);
			}
		}
	}
}
